package intelligentCustomer.planning;

import intelligentCustomer.intent.Intent;
import intelligentCustomer.session.UserSession;

import java.util.*;

/**
 * 规划上下文
 * 存储规划过程中的状态和中间结果
 */
public class PlanningContext {
    private final String query;
    private final Intent intent;
    private final UserSession session;
    private final Map<String, String> results = new HashMap<>();
    private final Map<String, Integer> retryCount = new HashMap<>();
    private final Map<String, Object> attributes = new HashMap<>();
    private int attempts = 0;
    private static final int MAX_ATTEMPTS = 5;
    
    public PlanningContext(String query, Intent intent, UserSession session) {
        this.query = query;
        this.intent = intent;
        this.session = session;
    }
    
    public void addResult(String stepName, String result) {
        results.put(stepName, result);
    }
    
    public String getResult(String stepName) {
        return results.get(stepName);
    }
    
    public void incrementRetryCount(String stepName) {
        retryCount.put(stepName, getRetryCount(stepName) + 1);
    }
    
    public int getRetryCount(String stepName) {
        return retryCount.getOrDefault(stepName, 0);
    }
    
    public void incrementAttempts() {
        attempts++;
    }
    
    public boolean hasExceededMaxAttempts() {
        return attempts >= MAX_ATTEMPTS;
    }
    
    public boolean isComplete() {
        return !results.isEmpty() && allStepsSuccessful();
    }
    
    private boolean allStepsSuccessful() {
        return results.values().stream().noneMatch(String::isEmpty);
    }
    
    public void setAttribute(String key, Object value) {
        attributes.put(key, value);
    }
    
    public Object getAttribute(String key) {
        return attributes.get(key);
    }
    
    // Getters
    public String getQuery() { return query; }
    public Intent getIntent() { return intent; }
    public UserSession getSession() { return session; }
    public Map<String, String> getResults() { return new HashMap<>(results); }
} 